
import LXCOM from '../../../common/common.js'
import tezhengbuzhuosvc from '../../../service/tezhengbuzhuosvc.js';

import plotsvcPkg from '../plot/plotservice.js'
var plotsvc = plotsvcPkg.PlotSvc;

import { AlignItemToContainerSvc } from '../../../service/dragdrop/alignItemToContainer.js';
import {LGXEnums}  from '../../../common/sysenum.js';

import utilfuncs from '../../../common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;

import globalConst from '../../../common/globalConst.js';
var global = globalConst.GlobalConst;

import { CrossfyLineSvc } from '../../../service/crossfylinesvc.js';

var LeftMoseUpTool = {

}

LeftMoseUpTool.action = function (dataManger, mosEvnt, worldPos) {
    var pCurentGraph = dataManger.getCurentGraph();
    if (pCurentGraph){
        dataManger.recordRepaintScope(pCurentGraph);
        pCurentGraph.setModifing(0);
    }
    if (dataManger.getDragFlag()
        && pCurentGraph != null
        && pCurentGraph.getStart() == 1
        && pCurentGraph.getUseful() == 1
        && pCurentGraph.getFinish() == 1) {
        this.correctGraphPos(dataManger, mosEvnt, worldPos);
        let mayConnGraphList = this.conn2OtherGraph(pCurentGraph, dataManger);
        var list = [];
        list.push(pCurentGraph);
        if (mayConnGraphList) {
            mayConnGraphList.forEach(ele => {
                if (list.indexOf(ele) == -1) {
                    list.push(ele);
                }
            })
        }
        AlignItemToContainerSvc.layContainerItemGraphInContainer(pCurentGraph, worldPos, dataManger);
        CrossfyLineSvc.crossGraphConneLine(pCurentGraph, dataManger);
        dataManger.m_pOperStepService.record(0, list);
    }
    else{
        if (dataManger.getOperStepStartFlag()){
            dataManger.m_pOperStepService.removePreStep();
        }
        if (pCurentGraph && pCurentGraph.getSelect() == 0){
            this.setCurentGraphTransKind(pCurentGraph, worldPos, dataManger.getScaling(), dataManger.getMapScale());
        }
    }
    dataManger.setDragFlag(false);
    let auxDataMng = dataManger.getAuxDataManager();
    auxDataMng.setSameSizeGroup(null, null, 0);
    auxDataMng.setSameSizeGroup(null, null, 1);
    if (pCurentGraph){
        dataManger.recordRepaintScope(pCurentGraph);
    }
}

LeftMoseUpTool.setCurentGraphTransKind = function (pGraph, tPoint, scaling, mapscale) {
    var tMakePoint = new LXCOM.LGXPoint();
    tMakePoint.clone(tPoint);
    let selKind = pGraph.calculateSelKind(tMakePoint, scaling, mapscale, false);
    if (selKind > 0) {
        pGraph.setSelect(pGraph.getSelect() + 1);
        pGraph.setSelKind(selKind);
    }
    else {
        pGraph.setSelect(0);
        pGraph.setSelKind(0);
    }
}

LeftMoseUpTool.conn2OtherGraph = function (pCurentGraph, dataManger) {
    let scaling = dataManger.getScaling();
    let mayConnGraphList = [];
    if (pCurentGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type) {
        mayConnGraphList = this.connLine2OtherGraphs(dataManger, pCurentGraph, scaling);
    }
    else {
        mayConnGraphList = this.connNoneLine2OtherGraphs(dataManger, pCurentGraph, scaling);
    }
    if (mayConnGraphList && mayConnGraphList.length > 0) {
        return mayConnGraphList;
    }
    else if (pCurentGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type) {
        this.reCorrectLineGraphConn(pCurentGraph, scaling);
    }
    else {
        this.reCorrectNoneLineGraphConn(pCurentGraph);
    }
    return mayConnGraphList;
}

LeftMoseUpTool.connLine2OtherGraphs = function (dataManger, pCurentGraph, scaling) {
    let topoMng = dataManger.getTopoManager();
    let position = pCurentGraph.getPosCopy();
    let headPt = position.getHead();
    let tailPt = position.getTail();

    let mayConnGraphList = [];
    let connGraph1 = dataManger.pickGraph(headPt, "blockGraph");
    if (connGraph1 && connGraph1 != pCurentGraph) {
        plotsvc.connect2DstGraph(topoMng, headPt, pCurentGraph, connGraph1, false, scaling);
        mayConnGraphList.push(connGraph1);
    }

    let connGraph2 = dataManger.pickGraph(tailPt, "blockGraph");
    if (connGraph2 && connGraph2 != pCurentGraph) {
        plotsvc.connect2DstGraph(topoMng, tailPt, pCurentGraph, connGraph2, false, scaling);
        mayConnGraphList.push(connGraph2);
    }
    return mayConnGraphList;
}

LeftMoseUpTool.connNoneLine2OtherGraphs = function (dataManger,pCurentGraph,scaling) {
    let topoMng = dataManger.getTopoManager();
    let mayConnGraphList = [];
    var tFeaturePTLst = [];

    pCurentGraph.getMainFeaturePTLst(tFeaturePTLst);
    for (let i = 0; i < tFeaturePTLst.length; i++) {
        let pt = tFeaturePTLst[i].pt;
        let connGraph = dataManger.pickGraph(pt, "linegraph");
        if (connGraph && connGraph != pCurentGraph) {
            plotsvc.connect2DstGraph(topoMng, pt, pCurentGraph, connGraph, false, scaling);
            mayConnGraphList.push(connGraph);
        }
    }
    return mayConnGraphList;
}


LeftMoseUpTool.reCorrectLineGraphConn = function (pCurentGraph, scaling) {
    let topoRes = pCurentGraph.getTopoRes();
    if (!topoRes) {
        return;
    }
    let position = pCurentGraph.getPosCopy();
    let headPt = position.getHead();
    let node1 = topoRes.stNode;
    if (node1) {
        let devRes1 = node1.getRefRes();
        if (devRes1){
            this.reCorrectNodeConnInfo(topoRes, headPt, node1, devRes1, scaling);
        }
    }

    let tailPt = position.getTail();
    let node2 = topoRes.endNode;
    if (node2) {
        let devRes2 = node2.getRefRes();
        if (devRes2){
            this.reCorrectNodeConnInfo(topoRes, tailPt, node2, devRes2, scaling);
        }
    }
}

LeftMoseUpTool.reCorrectNodeConnInfo = function (lineTopo, point, node, devRes, scaling) {
    let devGraph = devRes.getRefGraph();
    if (!devGraph) {
        return;
    }
    let rect = devGraph.getZone();
    rect.expand(global.SelectGap / scaling);
    if (!funcs.chkPtInRect(point, rect)) {
        lineTopo.removeNode(node);
        devRes.removeNode(node);
        node.setUseful(0);
    }
}

LeftMoseUpTool.reCorrectNoneLineGraphConn = function (pCurentGraph) {
    let topoRes = pCurentGraph.getTopoRes();
    if (!topoRes) {
        return;
    }
    let nodeList = topoRes.getConnNodeList();
    for (let i = 0; i < nodeList.length; i++) {
        let node = nodeList[i];

    }
}

LeftMoseUpTool.correctGraphPos = function (dataManger, mosEvnt, tPoint) {
    var offsetPt = new LXCOM.LGXPoint();
    let tmpGap = tezhengbuzhuosvc.TezhengBuzhuoSvc.absorbMouse2OtherGraphFeature(dataManger, tPoint, offsetPt,false);
    if (tmpGap) {
        if (tmpGap.xabsorb) {
            offsetPt.x = tmpGap.offset.x;
        }
        if (tmpGap.yabsorb) {
            offsetPt.y = tmpGap.offset.y;
        }
    }
    let nModifyInfo = 0;
    let dis = offsetPt.x * offsetPt.x + offsetPt.y * offsetPt.y;
    if (dis > global.ZERO_8E) {
        nModifyInfo = this.modifyCurentGraph(dataManger, offsetPt, mosEvnt);
    }
    return nModifyInfo;
}


LeftMoseUpTool.modifyCurentGraph = function (dataManger, offsetPt, mosEvnt) {
    var pCurentGraph = dataManger.getCurentGraph();
    if (pCurentGraph == null) {
        return 0;
    }

    if (pCurentGraph.getSelKind() == 0) {
        return 0;
    }

    var shiftdown = mosEvnt.shiftKey;
    var ctrlKeyDown = mosEvnt.ctrlKey;
    this.modifyGraphByOffset(dataManger, pCurentGraph, offsetPt, false, shiftdown, ctrlKeyDown);
    return 1;
}

LeftMoseUpTool.modifyGraphByOffset = function (dataManger, pGraph, offsetPt, shiftdown, ctrldown) {
    var cmdinfo = new LXCOM.LXModifyCMDInfo();
    cmdinfo.mapscale = dataManger.getMapScale();
    cmdinfo.scaling = dataManger.getScaling();
    cmdinfo.ctrldown = ctrldown;
    cmdinfo.shiftdown = shiftdown;
    var failed = pGraph.modifyByOffset(offsetPt, cmdinfo);
    return failed;
}

export default { LeftMoseUpTool }